<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2005
sirfragalot@users.sourceforge.net
**********************************************************************************/
/**
*BOOKMARK class
* Manage user's bookmarks.
*
* @author Mark Grimshaw
* 
* $Header: /cvsroot/wikindx/wikindx3/core/user/BOOKMARK.php,v 1.5 2007/07/15 18:16:25 sirfragalot Exp $
*/
class BOOKMARK
{
// Constructor
	function BOOKMARK($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/messages/SUCCESS.php");
		$this->success = new SUCCESS();
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
	}
// If $noMenu, display bookmark submenu links
	function display($noMenu = FALSE)
	{
		$this->template->setVar('heading', $this->messages->text("heading", "bookmark"));
		if($noMenu)
		{
			include_once("core/html/MENU.php");
			$menu = new MENU();
			$pString = $menu->noMenu('bookmark');
			$this->template->setVar('noMenu', $pString);
			return $this->template->process();
		}
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Display form for adding a bookmark
	function addInit($error = FALSE)
	{
		$this->template->setVar('heading', $this->messages->text("heading", "bookmark"));
		$pString = '';
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= MISC::p($this->messages->text("misc", "bookmark"));
		$pString .= FORM::formHeader("bookmarkAdd");
		$pString .= MISC::p(FORM::textInput($this->messages->text("misc", "bookmarkName"), 
			"name", FALSE, 16, 15));
		$bookmarks = $this->session->getArray("bookmark");
		if(is_array($bookmarks)) // no space left so display list for replacement
		{
			$max = TRUE;
			for($i = 1; $i <= 20; $i++)
			{
				if(!array_key_exists($i . "_name", $bookmarks))
				{
					$max = FALSE;
					break;
				}
			}
			if($max)
			{
				$pString .= MISC::p($this->messages->text("misc", "bookmarkDelete"));
				$checked = TRUE;
				for($i = 1; $i <= 20; $i++)
				{
					if(array_key_exists($i . "_name", $bookmarks))
					{
						$pString .= MISC::p(FORM::radioButton(FALSE, "bookmark_replace", $i, $checked) . 
						"&nbsp;&nbsp;" . stripslashes($bookmarks[$i . "_name"]));
						$checked = FALSE;
					}
				}
			}
		}
		$pString .= FORM::formSubmit('Add');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Add a bookmark
	function add()
	{
		if(!array_key_exists("name", $this->vars))
			$this->badInput($this->errors->text("inputError", "missing"), 'addInit');
		$name = trim($this->vars['name']);
		if(!$name)
			$this->badInput($this->errors->text("inputError", "missing"), 'addInit');
		$bookmarks = $this->session->getArray("bookmark");
		$id = 1; // default
		if(array_key_exists('bookmark_replace', $this->vars))
			$id = $this->vars['bookmark_replace'];
		else
		{
			if(is_array($bookmarks))
			{
				for($i = 1; $i <= 20; $i++)
				{
					if(!array_key_exists($i . "_name", $bookmarks))
					{
						$id = $i;
						break;
					}
				}
			}
		}
		$this->template->setVar('heading', $this->messages->text("heading", "bookmark"));
		$success = $this->success->text("bookmark");
		$this->session->setVar("bookmark_" . $id . "_name", $name);
		if($this->session->getVar('sql_lastView') == 'solo')
		{
			$this->session->setVar("bookmark_" . $id . "_id", $this->session->getVar('sql_lastSolo'));
// send back to view this resource with success message
			include_once("core/resource/RESOURCEVIEW.php");
			$resource = new RESOURCEVIEW($this->db, $this->vars);
			include_once("core/html/CLOSE.php");
			new CLOSE($this->db, $resource->view($success, 'lastSolo'));
		}
		else // multi view
		{
			$listParams = $this->session->getVar('sql_listParams') ? 
				'|' . $this->session->getVar('sql_listParams') : FALSE;
			$bookmark = base64_encode($this->session->getVar('sql_stmt')) . '|' . 
				base64_encode($this->session->getVar('sql_lastMulti')) . $listParams;
			if($this->session->getVar('sql_lastMulti') == 'powerSearch')
			{
				foreach($this->session->getArray("powerSearch") as $key => $value)
					$this->vars['powerSearch_' . $key] = base64_decode($value);
			}
			$this->session->setVar("bookmark_" . $id . "_multi", $bookmark);
// send back to view list with success message
			include_once("core/user/MYWIKINDX.php");
			$mw = new MYWIKINDX($this->db, $this->vars);
			include_once("core/html/CLOSE.php");
			new CLOSE($this->db, $mw->chooseDisplay($success));
		}
	}
// display bookmarks for deletion
	function deleteInit($error = FALSE)
	{
		$this->template->setVar('heading', $this->messages->text("heading", "bookmark"));
		$bookmarks = $this->session->getArray("bookmark");
		$bookmarkArray = array();
		$pString = '';
		if($error)
			$pString .= MISC::p($error, "error", "center");
		for($i = 1; $i <= 20; $i++)
		{
			if(array_key_exists($i . "_name", $bookmarks) && 
				array_key_exists($i . "_id", $bookmarks))
				$bookmarkArray[$i] = stripslashes($bookmarks[$i . "_name"]);
			else if(array_key_exists($i . "_name", $bookmarks) && 
				array_key_exists($i . "_multi", $bookmarks))
				$bookmarkArray[$i] = stripslashes($bookmarks[$i . "_name"]);
		}
		if(empty($bookmarkArray)) // none left
		{
			$this->template->setVar('body', $pString);
			return $this->template->process();
		}
		$pString .= FORM::formHeader("bookmarkDelete");
		$pString .= FORM::selectFBoxValueMultiple($this->messages->text("misc", "bookmarkDeleteInit"), 
			"bookmark_id", $bookmarkArray, 20) . MISC::br() . 
			$this->messages->text('hint', 'multiples') . MISC::br() . FORM::formSubmit('Delete');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Delete bookmark(s)
	function delete()
	{
		if(!array_key_exists("bookmark_id", $this->vars))
			$this->badInput($this->errors->text("inputError", "missing"), 'deleteInit');
		$bookmarks = $this->session->getArray("bookmark");
		for($i = 1; $i <= 20; $i++)
		{
			if(array_search($i, $this->vars['bookmark_id']) !== FALSE)
			{
				if(array_key_exists($i . "_id", $bookmarks))
					$this->session->delVar('bookmark_' . $i . '_id');
				else if(array_key_exists($i . "_multi", $bookmarks))
					$this->session->delVar('bookmark_' . $i . '_multi');
				$this->session->delVar('bookmark_' . $i . '_name');
			}
		}
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $this->deleteInit($this->success->text('bookmarkDelete')));
	}
// view a multi list bookmark
	function multiView()
	{
		if(!array_key_exists("id", $this->vars))
			die($this->errors->text("inputError", "missing"));
		$bookmarks = $this->session->getArray("bookmark");
		if(!array_key_exists($this->vars['id'] . '_multi', $bookmarks))
			die($this->errors->text("inputError", "missing"));
		$bookmark = explode('|', $bookmarks[$this->vars['id'] . '_multi']);
		$this->session->setVar('bookmark_lastMultiSql', $bookmark[0]);
		$this->session->setVar('bookmark_lastMultiType', $bookmark[1]);
		$stmt = stripslashes(base64_decode($bookmark[0]));
		$lastMulti = base64_decode($bookmark[1]);
//		$this->session->setVar('sql_stmt', base64_decode($bookmark[0]));
//		$this->session->setVar('sql_lastMulti', base64_decode($bookmark[1]));
		if(array_key_exists(2, $bookmark))
			$this->session->setVar('sql_listParams', $bookmark[2]);
		else
			$this->session->delVar('sql_listParams');
		$this->session->setVar('bookmark_multiView', TRUE);
		include_once("core/list/LISTCOMMON.php");
		$obj = new LISTCOMMON($this->db, $this->vars);
		$order = FALSE;
		$where = TRUE;
		if($lastMulti == 'select')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "select"));
			$queryString = "action=selectProcess&bookmark=1";
			$order = $this->session->getVar('select_order');
		}
		else if($lastMulti == 'statistics')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "select"));
			$queryString = "action=selectProcess&bookmark=1";
			$order = $this->session->getVar('select_order');
		}
		else if($lastMulti == 'list')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "list"));
			$queryString = "action=listProcess&bookmark=1";
			$order = $this->session->getVar('list_method');
			$where = FALSE;
		}
		else if($lastMulti == 'search')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "search"));
			$queryString = "action=searchProcess&bookmark=1";
			$order = $this->session->getVar('search_order');
		}
		else if($lastMulti == 'powerSearch')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "search"));
			$queryString = "action=powerSearchProcess&bookmark=1";
			$order = $this->session->getVar('powerSearch_order');
		}
		else if($lastMulti == 'listCategory')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "list"));
			$queryString = "action=listCategoryProcess&bookmark=1";
			$order = 'creator';
		}
		else if($lastMulti == 'listCreator')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "list"));
			$queryString = "action=listCreatorProcess&bookmark=1";
			$order = 'creator';
		}
		else if($lastMulti == 'listPublisher')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "list"));
			$queryString = "action=listPublisherProcess&bookmark=1";
			$order = 'creator';
		}
		else if($lastMulti == 'listCollection')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "list"));
			$queryString = "action=listCollectionProcess&bookmark=1";
			$order = 'creator';
		}
		else if($lastMulti == 'listKeyword')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "list"));
			$queryString = "action=listKeywordProcess&bookmark=1";
			$order = 'creator';
		}
		else if($lastMulti == 'listCite')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "list"));
			$queryString = "action=listCiteProcess&bookmark=1";
			$order = 'creator';
		}
		else if($lastMulti == 'basket')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "basket"));
			$queryString = "action=basketView&bookmark=1";
			$order = 'creator';
		}
		list($stmt, $pagingObject) = $obj->pagingStyle($stmt, $order, $where);
		list($pString, $pagingString) = $obj->listResources($stmt, $queryString, $lastMulti, FALSE, FALSE, $pagingObject);
		$this->template->setVar('body', $pString);
		$this->template->setVar('paging', $pagingString);
		return $this->template->process();
	}
// bad Input function
	function badInput($error, $method)
	{
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $this->$method($error));
	}
}
?>
